package at.fpmedv.eclipse.helma.console;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.ui.console.FileLink;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.ui.console.IPatternMatchListenerDelegate;
import org.eclipse.ui.console.PatternMatchEvent;
import org.eclipse.ui.console.TextConsole;

public class PatternMatchListenerDelegate implements IPatternMatchListenerDelegate {
	// [2009/03/22 13:51:52] [xss_server-1] [error] get:/ helma.scripting.ScriptingException: TypeError: Cannot read property "terrible" from undefined (/home/fpm/helma/apps/xss_server/code/HopObject/actions.js#7) helma.scripting.ScriptingException: TypeError: Cannot read property "terrible" from undefined (/home/fpm/helma/apps/xss_server/code/HopObject/actions.js#7)
	private static Pattern[] PATTERNS = new Pattern[] {
		Pattern.compile("\\(([^#]+#\\d+)\\)"),
	};
	
	/**
	 * The console associated with this line tracker 
	 */
	private TextConsole tConsole;

    /* (non-Javadoc)
     * @see org.eclipse.ui.console.IPatternMatchListenerDelegate#connect(org.eclipse.ui.console.IConsole)
     */
    public void connect(TextConsole console) {
	    tConsole = console;
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.console.IPatternMatchListenerDelegate#disconnect()
     */
    public void disconnect() {
        tConsole = null;
    }
    
    protected TextConsole getConsole() {
        return tConsole;
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.console.IPatternMatchListenerDelegate#matchFound(org.eclipse.ui.console.PatternMatchEvent)
     */
    public void matchFound(PatternMatchEvent event) {
    	if (tConsole != null) {
			int offset = event.getOffset();
			int length = event.getLength();
			String exceptionName;
    	    try {
				exceptionName = tConsole.getDocument().get(offset, length);
				if (exceptionName == null) {
					return;
				}
			} catch (BadLocationException e) {
				return;
			}
    	    int start = 0;	            
			for (int i = 0; i < PATTERNS.length; i++) {
        	    Pattern pattern = PATTERNS[i];
        	    Matcher matcher = pattern.matcher(exceptionName);
        	    while(matcher.find(start)) {
    				length = matcher.end(1) - matcher.start(1);
    				String link = exceptionName.substring(matcher.start(1), matcher.end(1));
    				if (link != null) {
    					try {
    						String[] items = link.split("#");
    						// create IFile
    			            IPath path = Path.fromOSString(items[0]);
    			            IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
    			            if (file != null) {
	    			            int lineNumber = -1;
	    			            try {
	    			            	lineNumber = Integer.parseInt(items[1]);
	    			            } catch(NumberFormatException e) {
	    			            	// ignore
	    			            }
	    			            if (file.isAccessible()) {
	    			            	System.out.println("#lineNumber: " + lineNumber);
		    			            FileLink fileLink = new FileLink(file, null, -1, -1, lineNumber);
									tConsole.addHyperlink(fileLink, offset + matcher.start(1), length);
	    			            }
    			            } else {
    			            	// open from filestore without linenumber
    			            }
						} catch (BadLocationException e) {
						}
    					return;
    				}
    				start = matcher.end();
        	    }
			}
    	}
    }
}
